package com.linkhub.oms.data.sync.api.shopify.domain.response;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.util.QtStreamUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;

import static com.linkhub.oms.common.util.OffSetDateTimeUtil.convertISO8601ToUTCDateTime;

/**
 * @Description: ShopProductResponse
 * @Author: echo
 * @Date: 2024/3/23 09:17
 * @Version: 1.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class ShopProductResponse extends BaseResponse implements Serializable {

    public static final String FIELDS = "id,title,created_at,updated_at,status,variants,images,image";


    /**
     * products
     */
    @JSONField(name = "products")
    private List<Products> products;

    @Override
    @JSONField(serialize = false, deserialize = false)
    public boolean success() {
        return CollUtil.isNotEmpty(products);
    }

    public void addAll(List<Products> products) {

        QtStreamUtil.peek(products,
                order -> order.setUpdatedTime(convertISO8601ToUTCDateTime(order.getUpdatedAt())));
        CollUtil.addAll(this.getProducts(), products);
        Collection<Products> distinct = QtStreamUtil.toMap(this.getProducts(),
                Products::getId, Function.identity()).values();
        this.setProducts(Lists.newArrayList(distinct));

        this.getProducts().sort(Comparator.comparing(Products::getUpdatedTime));

    }

    @Override
    @JSONField(serialize = false, deserialize = false)
    public int count() {
        return CollUtil.size(this.getProducts());
    }

    @JSONField(serialize = false, deserialize = false)
    public int getSkuCount() {
        List<Products.Variants> variants = QtStreamUtil.flatMap(products, product -> product.getVariants().stream());
        return CollUtil.size(variants);

    }

    @Override
    public String maxUpdateTime() {
        List<Products> products = this.getProducts();
        return products.get(products.size() - 1).getUpdatedAt();
    }

    public static ShopProductResponse emptyResponse() {
        ShopProductResponse res =  new ShopProductResponse();
        res.setProducts(Lists.newArrayList());
        return res;
    }

    /**
     * Products
     */
    @NoArgsConstructor
    @Data
    public static class Products {
        /**
         * id
         */
        @JSONField(name = "id")
        private Long id;
        /**
         * title
         */
        @JSONField(name = "title")
        private String title;
        /**
         * createdAt
         */
        @JSONField(name = "created_at")
        private String createdAt;
        /**
         * updatedAt
         */
        @JSONField(name = "updated_at")
        private String updatedAt;

        /**
         * 通过 updatedAt 转换
         */
        @JsonIgnore
        @JSONField(serialize = false, deserialize = false)
        private LocalDateTime updatedTime;
        /**
         * status
         */
        @JSONField(name = "status")
        private String status;
        /**
         * variants
         */
        @JSONField(name = "variants")
        private List<Variants> variants;
        /**
         * images
         */
        @JSONField(name = "images")
        private List<Images> images;

        /**
         * image
         */
        @JSONField(name = "image")
        private Image image;

        /**
         * Variants
         */
        @NoArgsConstructor
        @Data
        public static class Variants {
            /**
             * id
             */
            @JSONField(name = "id")
            private Long id;
            /**
             * productId
             */
            @JSONField(name = "product_id")
            private Long productId;
            /**
             * title
             */
            @JSONField(name = "title")
            private String title;
            /**
             * price
             */
            @JSONField(name = "price")
            private String price;
            /**
             * sku
             */
            @JSONField(name = "sku")
            private String sku;
            /**
             * createdAt
             */
            @JSONField(name = "created_at")
            private String createdAt;
            /**
             * updatedAt
             */
            @JSONField(name = "updated_at")
            private String updatedAt;
            /**
             * imageId
             */
            @JSONField(name = "image_id")
            private Long imageId;
        }

        /**
         * Images
         */
        @NoArgsConstructor
        @Data
        public static class Images {
            /**
             * id
             */
            @JSONField(name = "id")
            private Long id;

            /**
             * productId
             */
            @JSONField(name = "product_id")
            private Long productId;
            /**
             * createdAt
             */
            @JSONField(name = "created_at")
            private String createdAt;
            /**
             * updatedAt
             */
            @JSONField(name = "updated_at")
            private String updatedAt;

            /**
             * width
             */
            @JSONField(name = "width")
            private Integer width;
            /**
             * height
             */
            @JSONField(name = "height")
            private Integer height;
            /**
             * src
             */
            @JSONField(name = "src")
            private String src;
        }


        /**
         * Image
         */
        @NoArgsConstructor
        @Data
        public static class Image {
            /**
             * id
             */
            @JSONField(name = "id")
            private Long id;
            /**
             * alt
             */
            @JSONField(name = "alt")
            private Object alt;
            /**
             * position
             */
            @JSONField(name = "position")
            private Integer position;
            /**
             * productId
             */
            @JSONField(name = "product_id")
            private Long productId;

            /**
             * src
             */
            @JSONField(name = "src")
            private String src;
        }


    }
}
